
AIDS.LogHooks = { "PlayerAuthed",
"PlayerDisconnected",
"PlayerSay",
"PlayerDeath" }

AIDS.LogFormats = { "%s connected (Steam ID: %s).\n",
"%s disconnected.",
"%s: %s.",
"%s was killed with %s by %s."}

AIDS.ActiveHooks = {}

if CLIENT then
	
	local function ToggleLogHook( hook, value )
	
		RunConsoleCommand( "aids_toggle_log_hook", hook, value )
		
		AIDS.ActiveHooks[hook] = value
	
	end
	
	local function MakePanel()

		local panel = vgui.Create( "DPanel" )
		panel.CheckList = {}
		panel.InvalidateLayout = function()
		
			if panel.CheckList then
				
				local ypos = 10
				
				for k,v in pairs( panel.CheckList ) do
				
					v:SetPos( 5, ypos )
					
					ypos = ypos + 30
				
				end
				
			end
		
		end
		
		for k,v in pairs( AIDS.LogHooks ) do
		
			local box = vgui.Create( "DCheckBoxLabel", panel )
			box:SetText( v )
			box:SetValue( 1 )
			box:SetWide( 200 )
			box:SetTextColor( Color( 0, 0, 0, 255 ) )
			box.Button.OnChange = function( pnl, value )
			
				RunConsoleCommand( "aids_toggle_log_hook", k, value )
			
			end
			
			table.insert( panel.CheckList, box )
			
		end
		
		return panel
		
	end
	
	local function PopulateList( panel )
	
		local list = panel.CheckList
		
		if not list then return end
	
		for k,v in pairs( panel.CheckList ) do
		
			if AIDS.ActiveHooks[ v:GetText() ] == nil or AIDS.ActiveHooks[ v:GetText() ] == true then
			
				v:SetValue(1)
			
			else
			
				v:SetValue(0)
		
			end
		
		end
		
	end
	
	AIDS.AddMenuTab( "Logging", "gui/silkicons/page_white_wrench", "Manage which gamemode hooks are logged by the server.", MakePanel, PopulateList )

else

	AIDS.LogTable = {}
	AIDS.LogTime = 0
	AIDS.LogInterval = 120
	
	local function LogHook( str, ... )
	
		local tbl = {}
	
		for k,v in pairs{...} do
		
			if type( v ) == "Player" and ValidEntity( v ) then
			
				table.insert( tbl, v:Name() )
			
			elseif ( type( v ) == "NPC" or type( v ) == "Weapon" ) and ValidEntity( v ) then
			
				table.insert( tbl, v:GetClass() )
			
			else

				table.insert( tbl, tostring(v) )
			
			end
		
		end
	
		local str = Format( str, unpack( tbl ) )
		
		table.insert( AIDS.LogTable, str )
	
	end
	
	for k,v in pairs( AIDS.LogHooks ) do
	
		local function func( ... )
		
			if AIDS.ActiveHooks[v] == true or AIDS.ActiveHooks[v] == nil then
			
				LogHook( AIDS.LogFormats[k], ... )
			
			end
		
		end
	
		AIDS.Hook( v, "logging", func )
	
	end
	
	local function WriteToFile()
	
		local str = ""
		local today = os.date( "%d%b%Y-h%H" )
		local stamp = "Time Stamp: " .. os.date( "%H %M %S" )
		
		table.insert( AIDS.LogTable, stamp )
		
		if file.Exists( "AIDS/logs/" .. today .. ".txt" ) then
		
			str = file.Read( "AIDS/logs/" .. today .. ".txt" )
		
		end
	
		for k,v in pairs( AIDS.LogTable ) do
		
			str = str .. v .. "\n"
		
		end
		
		AIDS.LogTable = {}
		
		file.Write( "AIDS/logs/" .. today .. ".txt", str )
		
		print( "[AIDS] Updated server log file." )
	
	end
	
	local function LogThink( ply )
	
		if AIDS.LogTime < CurTime() then
		
			AIDS.LogTime = CurTime() + AIDS.LogInterval
			
			WriteToFile()
		
		end
	
	end
	
	AIDS.Hook( "Think", "logging", LogThink )
	
	local function ToggleLog( ply, cmd, args )
	
		if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) or not args[1] or not args[2] then return end
		
		AIDS.ActiveHooks[ args[1] ] = tobool( args[2] )
	
	end
	concommand.Add( "aids_toggle_log_hook", ToggleLog )
	
end